import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { BusinessMInfo, DeviceMAccess } from "@/api/backend/index";
import {
  getSum,
  splitEquipmentsRunTime,
  getKey,
  getAddr,
  getWarn,
} from "@/utils/common";

export const useBigBoardB = defineStore(Names.BigBoardB, {
  state: () => {
    return {
      devSum: 0, //设备总量
      point: 0,
      runTimeKeyArr: <any>[], //设备运行名称数组
      runTimeData: <any>[],
      warnData: <any>[],
      markers: <any>[], //设备地图坐标点位
    };
  },
  getters: {},
  actions: {
    //大屏2设备故障信息 改为事件
    getDevicesEvent(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/equipmentinformation",
          { region: [filter] + "" },
          "",
          ""
        ).then((res: any) => {
          let devicesEvent: { name: any; value: any }[] = [];
          res.rows.map((item: any) => {
            let data = {
              name:
                item.open_state === 0
                  ? "关闭"
                  : item.open_state === 1
                  ? "开启"
                  : "告警",
              value: item.value,
            };
            devicesEvent.push(data);
          });
          resolve(devicesEvent);
        });
      });
    },
    //设备
    getAbnormal(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/abnormallist",
          { region: [filter] + "" },
          "",
          ""
        ).then((res: any) => {
          resolve(res.rows);
        });
      });
    },
    //设备区域分布数据 默认常州不变
    getPoints(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/pointstatistics",
          { region: [filter] + "" },
          "",
          ""
        ).then(async (res: any) => {
          this.point = await getSum(res.rows); //区域设备总量
          let dataNew: { name: any; value: any }[] = [];
          res.rows.map((item: any) => {
            let data = {
              name: item.area_name,
              //   value: item.value ? item.value : null,
              value: item.value,
            };
            dataNew.push(data);
          });
          resolve(dataNew);
        });
      });
    },
    //设备排放量=运行时长
    getEquipmentruntime(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/equipmentruntime",
          { region: [filter] + "" },
          "",
          ""
        ).then((res: any) => {
          splitEquipmentsRunTime(res.rows).then((res) => {
            this.runTimeData = res;
          });
          this.runTimeKeyArr = getKey(res.rows);
          resolve("");
        });
      });
    },
    //获取所有设备信息
    getAllDevices(filter: any, boo: Boolean) {
      return new Promise((resolve, reject) => {
        _get(DeviceMAccess + "/listBytree", filter, "json", "")
          .then(async (res: any) => {
            if (boo) {
              let markers = await getAddr(res.rows);
              this.markers = markers;
              resolve(markers);
            } else {
              resolve(res.rows);
            }
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //设备告警分类
    getEquipmentWarning(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "system/warning/equipmentwarningstatistics",
          { region: [filter] + "" },
          "",
          ""
        )
          .then(async (res: any) => {
            console.log(res);
            this.warnData = await getWarn(res.rows);
            resolve("");
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
